﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Security.Principal;
using MyNote.Data.Models;

namespace MyNote.Controllers
{
    [ValidateInput(false)]
    public class ReferenceNoteController : NoteController
    {
        public ReferenceNoteController(IPrincipal user)
            : base(user)
        {
        }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            if (!Request.IsAuthenticated && Request.Form.HasKeys() && !string.IsNullOrEmpty(Request.Form["Url"]))
            {
                var note = new ReferenceNote()
                {
                    Title = Server.UrlDecode(Request.Form["Title"]),
                    Url = Request.Form["Url"],
                    Description = Server.UrlDecode(Request.Form["Description"]),
                    Tag = Server.UrlDecode(Request.Form["Tag"])
                };
                Session[MvcApplication.Constants.PENDING_CREATE_NOTE] = note;
            }
        }
        
        [Authorize]
        public ActionResult BeginCreate()
        {
            var note = Session[MvcApplication.Constants.PENDING_CREATE_NOTE] as Note;
            return View(note);
        }

        [Authorize] 
        [HttpPost]
        public ActionResult BeginCreate(ReferenceNote note)
        {
            note.Title = Server.UrlDecode(note.Title);
            note.Description = Server.UrlDecode(note.Description);
            note.Tag = Server.UrlDecode(note.Tag);

            return Create(note);
        }
    }
}
